from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()


class UserEntity(db.Model):
    __tablename__ = 't_sys_user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    phone_no = db.Column(db.String(11), unique=True, nullable=False)
    username = db.Column(db.String(38), unique=False, nullable=True)
    password = db.Column(db.String(255), unique=False, nullable=False)
    referral_code = db.Column(db.String(10), info='推荐码')
    create_date = db.Column(db.DateTime, unique=False, nullable=False)

class TSysDict(db.Model):
    __tablename__ = 't_sys_dict'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    label = db.Column(db.String(500), server_default=db.FetchedValue(), info='字典标签')
    value = db.Column(db.String(500), server_default=db.FetchedValue(), info='字典值')
    type_code = db.Column(db.String(100), server_default=db.FetchedValue(), info='类型编码')
    sort = db.Column(db.Integer, server_default=db.FetchedValue(), info='排序')
    is_default = db.Column(db.String(1), server_default=db.FetchedValue(), info='是否默认（0否 1是）')
    remark = db.Column(db.String(255), server_default=db.FetchedValue(), info='备注')
    create_date = db.Column(db.DateTime, info='创建时间')



class TSysEpisode(db.Model):
    __tablename__ = 't_sys_episodes'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='所属用户主键')
    libretto_id = db.Column(db.Integer, nullable=False, info='所属剧本主键')
    episode_number = db.Column(db.Integer, nullable=False, info='分集编号')
    title = db.Column(db.String(255), nullable=False, info='分集标题')
    synopsis = db.Column(db.Text, nullable=False, info='分集梗概')
    outline = db.Column(db.Text, info='分集大纲')
    create_date = db.Column(db.DateTime, info='创建时间')



class TSysFile(db.Model):
    __tablename__ = 't_sys_files'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    correlation_id = db.Column(db.Integer, nullable=False, info='关联ID')
    correlation_type = db.Column(db.String(50), server_default=db.FetchedValue(), info='关联类型:novel-小说,scenario-剧本,shortscenario-短剧')
    correlation_sub_id = db.Column(db.Integer, nullable=False, info='关联子ID:类型为小说则子ID为章节ID、类型为短剧则子ID为分集剧本ID')
    name = db.Column(db.String(100), nullable=False, info='文件名')
    local_path = db.Column(db.String(255), nullable=False, info='文件本地路径（对应向量数据库中meta-source）')
    remote_path = db.Column(db.String(255), server_default=db.FetchedValue(), info='文件远程实际存储路径（文件在对象存储中的路径）')
    file_type = db.Column(db.String(50), server_default=db.FetchedValue(), info='文件类型:mp4,mp3,png,jpg')
    create_date = db.Column(db.DateTime, info='创建时间')
    update_date = db.Column(db.DateTime, info='更新时间')



class TSysLibretto(db.Model):
    __tablename__ = 't_sys_libretto'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='所属用户主键')
    name = db.Column(db.String(50), nullable=False, info='剧本名称')
    introduction = db.Column(db.Text, nullable=False, info='剧本简介')
    brief_biography = db.Column(db.Text, nullable=False, info='人物小传')
    outline = db.Column(db.String, nullable=False, info='剧本大纲')
    reference_text = db.Column(db.Text, info='参照文本')
    format = db.Column(db.String(500), nullable=False, info='剧本格式')
    status = db.Column(db.String(2), server_default=db.FetchedValue(), info='剧本状态（1-未完结，2-已完结）')
    type = db.Column(db.String(2), server_default=db.FetchedValue(), info='剧本类型（1-长剧，2-短剧）')
    novel_id = db.Column(db.Integer, info='小说主键')
    create_date = db.Column(db.DateTime, info='创建时间')



class TSysScene(db.Model):
    __tablename__ = 't_sys_scenes'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='所属用户主键')
    episodes_id = db.Column(db.Integer, nullable=False, info='所属分集主键')
    title = db.Column(db.String(255), nullable=False, info='分场标题')
    scene_number = db.Column(db.Integer, nullable=False, info='分场编号')
    elements = db.Column(db.Text, nullable=False, info='分场要素')
    synopsis = db.Column(db.Text, nullable=False, info='分场梗概')
    content = db.Column(db.Text, nullable=False, info='分场内容')
    create_date = db.Column(db.DateTime, info='创建时间')

class TSysTokenUsageHistory(db.Model):
    __tablename__ = 't_sys_token_usage_history'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    input_tokens = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='输入Token数，默认为0')
    output_tokens = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='输出Token数，默认为0')
    total_tokens = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='总Token数，默认为0')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysUserAccount(db.Model):
    __tablename__ = 't_sys_user_account'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    recharge_amount = db.Column(db.Numeric(15, 2), nullable=False, server_default=db.FetchedValue(), info='用户充值金额，默认为0.00，保留两位小数')
    token_type = db.Column(db.String(50), nullable=False, info='充值类型，1-日冲 2-月冲 3-季度冲 4-年冲')
    token_max_limit = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='Token最大上限，默认为0')
    create_date = db.Column(db.DateTime, info='账户创建时间，默认为当前时间戳')
    updated_date = db.Column(db.DateTime, info='账户最后更新时间，每次更新记录时自动设置为当前时间戳')

class TSysNovel(db.Model):
    __tablename__ = 't_sys_novel'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    outline_id = db.Column(db.Integer, nullable=False, info='小说大纲ID')
    name = db.Column(db.String(50), nullable=False, info='小说名称')
    introduction = db.Column(db.Text, info='小说简介')
    synopsis = db.Column(db.Text, info='小说大纲')
    status = db.Column(db.String(2), server_default=db.FetchedValue(), info='剧本状态（1-未完结，2-已完结）')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')


class TSysNovelOutline(db.Model):
    __tablename__ = 't_sys_novel_outline'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    name = db.Column(db.String(50), nullable=False, info='大纲名称')
    genre_type = db.Column(db.String(20), nullable=False, info='题材类型')
    historical_background = db.Column(db.String(20), nullable=False, info='题材历史背景')
    chapter_count = db.Column(db.Integer, info='章节数量')
    chapter_word_count = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='章节字数')
    other_restrictions = db.Column(db.Text, info='其他限制')
    character_settings = db.Column(db.Text, info='人物设定')
    story_outline = db.Column(db.Text, info='故事概要')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysNovelChapter(db.Model):
    __tablename__ = 't_sys_novel_chapters'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='所属用户主键')
    novel_id = db.Column(db.Integer, nullable=False, info='所属小说主键')
    title = db.Column(db.String(255), nullable=False, info='章节标题')
    number = db.Column(db.Integer, nullable=False, info='章节编号')
    synopsis = db.Column(db.Text, nullable=False, info='章节概要')
    content = db.Column(db.String, info='章节内容')
    hook = db.Column(db.Text, info='钩子(上一章写到)')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysPaymentOrder(db.Model):
    __tablename__ = 't_sys_payment_orders'

    id = db.Column(db.Integer, primary_key=True)
    order_no = db.Column(db.String(100), nullable=False, unique=True, info='订单编号')
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    order_status = db.Column(db.String(50), nullable=False, server_default=db.FetchedValue(), info='订单状态:pending,paid,cancelled,refunded,failed')
    order_amount = db.Column(db.Numeric(10, 2), nullable=False, info='订单金额')
    currency = db.Column(db.String(3), nullable=False, server_default=db.FetchedValue(), info='货币单位')
    description = db.Column(db.String(100), info='支付描述')
    quantity = db.Column(db.Integer, server_default=db.FetchedValue(), info='商品数量')
    transaction_id = db.Column(db.String(100), info='交易号')
    payer_openid = db.Column(db.String(100), info='支付人开放ID')
    pay_url = db.Column(db.String(100), info='支付链接')
    payment_method = db.Column(db.String(50), nullable=False, info='支付方式:wechat,alipay')
    payment_time = db.Column(db.DateTime, info='支付时间')
    create_time = db.Column(db.DateTime, info='创建时间')
    update_time = db.Column(db.DateTime, info='更新时间')

class TSysUserOperationLog(db.Model):
    __tablename__ = 't_sys_user_operation_logs'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, info='用户ID')
    operation_type = db.Column(db.String(100), nullable=False, info='操作类型')
    ip_address = db.Column(db.String(45), nullable=False, info='IP地址')
    device_info = db.Column(db.String(100), info='设备信息')
    referer = db.Column(db.String(500), info='跳转链接')
    operation_time = db.Column(db.DateTime, info='操作时间')

class TSysUserReferralCode(db.Model):
    __tablename__ = 't_sys_user_referral_codes'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, info='用户ID')
    referral_code = db.Column(db.String(10), nullable=False, unique=True, info='推荐码')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysUserFeedback(db.Model):
    __tablename__ = 't_sys_user_feedback'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, info='用户ID')
    feedback_content = db.Column(db.Text, nullable=False, info='反馈意见')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysCharacterAppearance(db.Model):
    __tablename__ = 't_sys_character_appearance'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='用户ID')
    correlation_id = db.Column(db.Integer, nullable=False, info='关联ID')
    correlation_type = db.Column(db.String(50), server_default=db.FetchedValue(), info='关联类型:novel-小说,scenario-剧本,shortscenario-短剧')
    character_appearance = db.Column(db.Text, nullable=False, info='人物样貌描述')
    create_date = db.Column(db.DateTime, info='创建时间')
    updated_date = db.Column(db.DateTime, info='更新时间')

class TSysVideo(db.Model):
    __tablename__ = 't_sys_videos'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, info='对应的用户id')
    novel_id = db.Column(db.Integer, nullable=False, info='对应的小说id')
    chapter_id = db.Column(db.Integer, nullable=False, info='对应的章节id')
    task_id = db.Column(db.String(100), nullable=False, info='生成视频时对应得任务id')
    file_id = db.Column(db.Integer, info='文件id，与t_sys_files表关联用')
    name = db.Column(db.String(100), nullable=False, info='视频名称-小说名称+章节名称+时间戳')
    file_name = db.Column(db.String(200), info='视频文件名称')
    video_type = db.Column(db.String(10), server_default=db.FetchedValue(), info='视频类型')
    cover_img_name = db.Column(db.String(200), info='封面图片名称')
    cover_img_base64 = db.Column(db.Text, info='封面图片的base64编码')
    create_stauts = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='创建状态 0: 未完成 1：创建中  2：完成  3：异常')
    create_error_code = db.Column(db.String(20), info='创建视频异常代码')
    create_progress = db.Column(db.Integer, server_default=db.FetchedValue(), info='创建进度')
    video_duration = db.Column(db.Integer, info='视频长度')
    resolution = db.Column(db.String(50), server_default=db.FetchedValue(), info='视频分辨率')
    has_checked_token = db.Column(db.String(50), server_default=db.FetchedValue(), info='是否已经支付token')
    create_time = db.Column(db.DateTime, nullable=False, info='创建时间')
    create_success_time = db.Column(db.DateTime, info='生成视频完毕时间')
    create_task_start_time = db.Column(db.DateTime, info='创建视频任务开始执行的时间（被MQ消费者消费开始）')

class TSysUserBlacklist(db.Model):
    __tablename__ = 't_sys_user_blacklist'

    id = db.Column(db.Integer, primary_key=True)
    phone_or_ip = db.Column(db.String(45), nullable=False, info='手机号或ip地址')
    create_date = db.Column(db.DateTime, info='创建时间')